﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Configuration;

namespace HCQ2_Common
{
    /// <summary>
    /// 获取Web.config下面的appSettings配置
    /// </summary>
    public class AppConfigAppSetting
    {
        /// <summary>
        /// 根据Key获取指定的值
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetName(string key)
        {
            string val = WebConfigurationManager.AppSettings[key];
            //Web的获取不到通过客户方式再获取一次
            if (!string.IsNullOrEmpty(val))
            {
                val = ConfigurationManager.AppSettings[key].ToString();
            }

            return val;
        }

        /// <summary>
        /// 设置Key值
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        /// <returns></returns>
        public static void SetNameVal(string key, string value)
        {
            var config = WebConfigurationManager.OpenWebConfiguration("~");
            config.AppSettings.Settings.Remove(key);
            config.AppSettings.Settings.Add(key, value);
            config.Save();
        }
    }
}